package com.score.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.score.pojo.ReturnEntry;
import com.score.pojo.ResponseEntry;
import com.score.pojo.TCheckDorm;
import com.score.pojo.TStudent;
import com.score.service.impl.CheckDormServiceImpl;
import com.score.service.impl.DormServiceImpl;
import com.score.service.impl.StudentServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;

@Api(value = "寝室信息管理", tags = "channel", description = "测试频道管理API")
@RestController
public class DormController {
    @Autowired
    private CheckDormServiceImpl checkDormService;

    @Autowired
    private StudentServiceImpl studentService;

    @Autowired
    private DormServiceImpl dormService;

    /**
     * 老师获取所有寝室信息
     *
     * @return
     */
    @ApiOperation("老师获取所有寝室信息")
    @Secured({"ROLE_headmaster", "ROLE_instructor", "ROLE_deputySecretary", "ROLE_dean"})
    @PostMapping("/checkdorm")
    public ReturnEntry getAllDorm(String college, String className, String position) {
        Set<String> allTime = checkDormService.getAllTime(college, className, position);
        ReturnEntry allDorm = checkDormService.getAllDorm(college, className, position);
        return new ReturnEntry().ok(allDorm.getData2(), allDorm.getData(), allTime);
    }

    /**
     * 老师页面筛选所有寝室信息
     *
     * @param check_time
     * @param state
     * @return
     */
    @ApiOperation("老师页面筛选所有寝室信息")
    @Secured({"ROLE_headmaster", "ROLE_instructor", "ROLE_deputySecretary", "ROLE_dean"})
    @PostMapping("/findBytime")
    public ReturnEntry findBycondition(String college, String className, String position, String check_time, String state, String isread, @RequestParam(defaultValue = "1") Integer currentPage, @RequestParam(defaultValue = "10") Integer pageSize) {
        return checkDormService.findBycondition(college, className, position, check_time, state, isread, currentPage, pageSize);
    }

    /**
     * 老师页面获取指定状态的寝室信息及次数
     *
     * @param state
     * @return
     */
    @ApiOperation("老师页面获取指定状态的寝室信息及次数")
    @Secured({"ROLE_headmaster", "ROLE_instructor", "ROLE_deputySecretary", "ROLE_dean"})
    @PostMapping("/getStateAndCount")
    public ReturnEntry getStateAndCount(String college, String className, String state,
                                        @RequestParam(defaultValue = "1") Integer currentPage,
                                        @RequestParam(defaultValue = "10") Integer pageSize) {
        return checkDormService.getStateAndCount(college, className, state, currentPage, pageSize);
    }

    /**
     * 老师根据对应次数获取寝室详细信息
     * @param dormNum
     * @param state
     * @param currentPage
     * @param pageSize
     * @return
     */
    @ApiOperation("老师根据对应次数获取寝室详细信息")
    @Secured({"ROLE_headmaster", "ROLE_instructor", "ROLE_deputySecretary", "ROLE_dean"})
    @PostMapping("/getDetailedMsg")
    public ReturnEntry getDetailedMsg(String dormNum, String state,
                                      @RequestParam(defaultValue = "1") Integer currentPage,
                                      @RequestParam(defaultValue = "10") Integer pageSize) {
        return checkDormService.getDetailedMsg(dormNum, state, currentPage, pageSize);
    }

    /**
     * 获取学生自己寝室信息及所有查寝时间
     *
     * @param
     * @return
     */
    @ApiOperation("获取学生自己寝室信息及所有查寝时间")
    @Secured("ROLE_student")
    @PostMapping("/stuDorm")
    public ReturnEntry getStuDorm(String stunum, @RequestParam(defaultValue = "1") Integer currentPage, @RequestParam(defaultValue = "10") Integer pageSize) {
        TStudent student = studentService.getStudentInfo(stunum);
        Set<String> allTime = checkDormService.getAllStuTime(student.getDormNum());
        Page<TCheckDorm> stuDorm = checkDormService.getStuDorm(student.getDormNum(), currentPage, pageSize);
        return new ReturnEntry().ok(stuDorm.getRecords(), allTime,stuDorm.getTotal());
    }

    /**
     * 老师修改寝室信息未读状态
     *
     * @param dorm
     * @return
     */
    @ApiOperation("老师修改寝室信息未读状态")
    @Secured({"ROLE_headmaster", "ROLE_instructor", "ROLE_deputySecretary", "ROLE_dean"})
    @PostMapping("/changeRead")
    public ReturnEntry changeReadDorm(TCheckDorm dorm, String position) {
        TCheckDorm tCheckDorm = checkDormService.changeReadDorm(dorm, position);
        return new ReturnEntry().ok(tCheckDorm, dormService.getDormByDnum(tCheckDorm.getDormNum()));
    }

    /**
     * 老师一键已读合格信息
     *
     * @param college
     * @param className
     * @param position
     * @return
     */
    @ApiOperation("老师一键已读合格信息")
    @Secured({"ROLE_headmaster", "ROLE_instructor", "ROLE_deputySecretary", "ROLE_dean"})
    @PostMapping("/allRead")
    public ReturnEntry allRead(String college, String className, String position) {
        return checkDormService.allRead(college, className, position);
    }

    /**
     * 老师修改寝室合格信息
     *
     * @param dorm
     * @return
     */
    @ApiOperation("老师修改寝室合格信息")
    @Secured("ROLE_instructor")
    @PostMapping("/changeState")
    public TCheckDorm changeStateDorm(TCheckDorm dorm) {
        return checkDormService.changeStateDorm(dorm);
    }

    /**
     * 老师查看反馈信息，改变反馈信息状态
     *
     * @param dorm
     * @return
     */
    @ApiOperation("老师查看反馈信息，改变反馈信息状态")
    @Secured({"ROLE_headmaster", "ROLE_instructor", "ROLE_deputySecretary", "ROLE_dean"})
    @PostMapping("/changeFbRead")
    public TCheckDorm changeFbRead(TCheckDorm dorm, String position) {
        return checkDormService.changeFbRead(dorm, position);
    }

    /**
     * 筛选学生自己寝室信息
     *
     * @param dormNum
     * @param check_time
     * @param state
     * @return
     */
    @ApiOperation("筛选学生自己寝室信息")
    @Secured("ROLE_student")
    @PostMapping("/findDorm")
    public ReturnEntry findBySome(String dormNum, String check_time, String state,
                                  @RequestParam(defaultValue = "1") Integer currentPage,
                                  @RequestParam(defaultValue = "10") Integer pageSize) {
        return checkDormService.findBySome(dormNum, check_time, state, currentPage, pageSize);
    }

    /**
     * 学生反馈给老师信息
     *
     * @param dorm
     * @return
     */
    @ApiOperation("学生反馈给老师信息")
    @Secured("ROLE_student")
    @PostMapping("/sendfeedback")
    public ResponseEntry sendFeedback(TCheckDorm dorm) {
        return checkDormService.sendFeedback(dorm);
    }

    /**
     * 学生改变寝室信息读取状态
     *
     * @param dorm
     * @return
     */
    @ApiOperation("学生改变寝室信息读取状态")
    @Secured("ROLE_student")
    @PostMapping("/changeStateStu")
    public ResponseEntry changeStateStu(TCheckDorm dorm) {
        return checkDormService.changeStateStu(dorm);
    }
}